* ==============================================================================
* Table 2. prediction models for network size using multilevel modeling
* ==============================================================================
gen syear = year - 1985 

gen smode = i_phonemode 
replace smode = 0 if year == 1985 | year == 1987  // don't know; probably face-to-face in most cases
replace smode = 2 if year == 2008 | ayear == 2011 | year == 2016 // internet
replace smode = 1 if year == 1992 
replace smode = 1 if year == 2006 

gen s_election = 0 
replace s_election = 1 if year == 1992 | year == 2000 | year == 2004 | year == 2008 | year == 2016 

* adjust education in 2008
replace r_educ = 9 if r_degree == 1 & year == 2008 & missing(r_educ)
replace r_educ = 12 if r_degree == 2 & year == 2008 & missing(r_educ)
replace r_educ = 14 if r_degree == 3 & year == 2008 & missing(r_educ)
replace r_educ = 16 if r_degree == 4 & year == 2008 & missing(r_educ)
replace r_educ = 18 if r_degree == 5 & year == 2008 & missing(r_educ)

* adjust imp name generator indicator
replace imp_name = i_imp if year == 2006 

* data set 
gen sdataset = 0 
replace sdataset = 1 if dataset == "GSS"
replace sdataset = 2 if dataset == "TESS"
replace sdataset = 3 if dataset == "CNES"

* basic descriptive stats.
sum r_female c.r_age r_white r_black c.r_educ r_married r_working imp_name sdataset s_election smode if year == 2006

* month indicator 
gen lastmonth = 0 
replace lastmonth = 1 if year == 2000
replace lastmonth = 1 if year == 2016

* survey design : probe name more than one name in the box
gen sdesign = 0 
replace sdesign = 1 if year == 2008 

* define analytic sample
reg n_size3 r_female c.r_age r_black r_others c.r_educ r_married r_working b4.r_partyid7 /*
 */ i.smode sdesign s_election##i.imp_name lastmonth 
gen anal_sample = e(sample)

gen myear = ayear 
replace myear = 1988 if ayear == 1987 & imp_name == 0
replace myear = 1993 if ayear == 1992 & imp_name == 0
replace myear = 2017 if ayear == 2016 & imp_name == 0 

xtset myear 

estimates clear 
xtreg n_size3 r_female c.r_age r_black r_others c.r_educ r_married r_working b4.r_partyid7 /*
 */ i.smode sdesign s_election##i.imp_name lastmonth, re, if year != 2016
estimates store m1 
xtreg n_size6 r_female c.r_age r_black r_others c.r_educ r_married r_working b4.r_partyid7 /*
 */ i.smode sdesign s_election##i.imp_name lastmonth, re, if year != 2016
estimates store m2 
esttab * using "./tables/table_s2_prediction.csv", csv replace star (+ 0.1 * 0.05 ** 0.01) se(%9.2f) nogap label b(%9.3f) 

* -----------------------------------
* For Figure 6
* -----------------------------------
xtreg n_size3 r_female c.r_age r_black r_others c.r_educ r_married r_working b4.r_partyid7 /*
 */ i.smode sdesign s_election##i.imp_name lastmonth, re, if year != 2016

predict xb_size3 if ~missing(n_size3)
predict error3, stdp, if ~missing(n_size3)
generate lb_size3 = xb_size3 - invnormal(0.975)*error3
generate ub_size3 = xb_size3 + invnormal(0.975)*error3

* mean for predicted network size
svy: mean xb_size3 if year == 2016 & imp_name == 1 & anal_sample == 1
svy: mean lb_size3 if year == 2016 & imp_name == 1 & anal_sample == 1
svy: mean ub_size3 if year == 2016 & imp_name == 1 & anal_sample == 1
svy: mean xb_size3 if year == 2016 & imp_name == 0 & anal_sample == 1
svy: mean lb_size3 if year == 2016 & imp_name == 0 & anal_sample == 1
svy: mean ub_size3 if year == 2016 & imp_name == 0 & anal_sample == 1

* mean for observed network size
svy: mean n_size3 if year == 2016 & imp_name == 0 & anal_sample == 1
svy: mean n_size3 if year == 2016 & imp_name == 1 & anal_sample == 1

* sensitivity checks. assume that network size = 6.
xtreg n_size6 r_female c.r_age r_black r_others c.r_educ r_married r_working b4.r_partyid7 /*
 */ i.smode sdesign s_election##i.imp_name lastmonth, re, if year != 2016 

predict xb_size6 if ~missing(n_size6)
predict error6, stdp, if ~missing(n_size6)
generate lb_size6 = xb_size6 - invnormal(0.975)*error6
generate ub_size6 = xb_size6 + invnormal(0.975)*error6

* mean for predicted network sizes
svy: mean xb_size6 if year == 2016 & imp_name == 1 & anal_sample == 1
svy: mean lb_size6 if year == 2016 & imp_name == 1 & anal_sample == 1
svy: mean ub_size6 if year == 2016 & imp_name == 1 & anal_sample == 1
svy: mean xb_size6 if year == 2016 & imp_name == 0 & anal_sample == 1
svy: mean lb_size6 if year == 2016 & imp_name == 0 & anal_sample == 1
svy: mean ub_size6 if year == 2016 & imp_name == 0 & anal_sample == 1

* mean for observed network sizes
svy: mean n_size6 if year == 2016 & imp_name == 1 & anal_sample == 1
svy: mean n_size6 if year == 2016 & imp_name == 0 & anal_sample == 1

